package com.itheima.springbootmybatis.mapper;

import com.itheima.springbootmybatis.pojo.FruitVegetables;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleMapper {
    //新增
    @Insert("insert into fruitsandvegetables(fruit_name,cover_img,origin,weight,product_features,price,merchant_id,create_time,update_time,status)" +
            " values (#{fruitName},#{coverImg},#{origin},#{weight},#{productFeatures},#{price},#{merchantId},#{createTime},#{updateTime},#{status})")
    void add(FruitVegetables fruitVegetables);

    //条件分页查询
    @Select("select * from fruitsandvegetables where origin LIKE CONCAT('%', #{origin}, '%') " +
            " AND fruit_name LIKE CONCAT('%', #{fruitName}, '%') and (status <>'0' OR merchant_id=#{userId})")
    List<FruitVegetables> list(Integer userId,String origin,String fruitName);

    //根据id查询
    @Select("select * from fruitsandvegetables where id=#{id}")
    FruitVegetables findById(Integer id);

    //更新文章
    @Update(" update fruitsandvegetables set fruit_name=#{fruitVegetables.fruitName},cover_img=#{fruitVegetables.coverImg}," +
            " origin=#{fruitVegetables.origin},weight=#{fruitVegetables.weight},product_features=#{fruitVegetables.productFeatures}," +
            " price=#{fruitVegetables.price}, update_time=#{fruitVegetables.updateTime},status=#{fruitVegetables.status} " +
            " where id=#{fruitVegetables.id} and merchant_id=#{userId}")
    void update(@Param("userId")Integer userId,@Param("fruitVegetables")FruitVegetables fruitVegetables);

    //删除文章
    @Delete("delete from fruitsandvegetables where id=#{id}")
    void deleteById(Integer id);
//根据id更新商品对应的商家
    @Update("UPDATE fruitsandvegetables AS f SET f.merchant_nickname = (SELECT m.nickname FROM merchant AS m  " +
            " WHERE m.id = f.merchant_id);")
    void updateNickname(Integer userId);
//编辑查看是否是自己的商品
    @Select("select * from fruitsandvegetables where fruitsandvegetables.id=#{fruitVegetables.id} " +
            " and fruitsandvegetables.merchant_id=#{userId}")
    String selectMy(Integer userId, FruitVegetables fruitVegetables);
//删除查看是否是自己的商品
    @Select("select * from fruitsandvegetables where fruitsandvegetables.id=#{id} " +
            " and fruitsandvegetables.merchant_id=#{userId}")
    String selectDeleteMy(Integer userId, Integer id);
}
